package com.heima.search.listener;



import com.heima.common.constants.mq.NewsUpOrDownConstants;
import com.heima.common.exception.CustException;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.vos.SearchArticleVO;
import com.heima.search.service.ApArticleSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class ArticleAddOrRemoveListener {
    @Resource
    ApArticleSearchService apArticleSearchService;

    @RabbitListener(queuesToDeclare = @Queue(name = NewsUpOrDownConstants.NEWS_UP_FOR_ES_QUEUE))
    public void addEs(String articleId){
        if (articleId == null){
            log.error("addEs监听出错，未取到articleId");
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
        }
        SearchArticleVO searchArticleVO = new SearchArticleVO();
        searchArticleVO.setId(Long.valueOf(articleId));
        apArticleSearchService.saveArticle(searchArticleVO);
        log.info("监听到addEs队列信息，saveArticle方法已执行");
    }


    @RabbitListener(queuesToDeclare = @Queue(name = NewsUpOrDownConstants.NEWS_DOWN_FOR_ES_QUEUE))
    public void delEs(String articleId){
        if (articleId == null){
            log.error("delEs监听出错，未取到articleId");
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
        }
        apArticleSearchService.deleteArticle(articleId);
    }
}
